package lanou.autohome.findcar.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import lanou.autohome.R;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.findcar.beans.MorkDownBean;

/**
 * Created by dllo on 16/8/25.
 */
public class MorkDownAdapter extends BaseAdapter{

    private Context mcontext;
    private MorkDownBean morkDownBean;

    public MorkDownAdapter(Context mcontext) {
        this.mcontext = mcontext;
    }

    public void setMorkDownBean(MorkDownBean morkDownBean) {
        this.morkDownBean = morkDownBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return morkDownBean != null ? morkDownBean.getResult().getCarlist().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder  = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.item_findcar_morkdown , null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MorkDownBean.ResultBean.CarlistBean carlistBean = morkDownBean.getResult().getCarlist().get(position);
        MorkDownBean.ResultBean.CarlistBean.DealerBean dealerBean = morkDownBean.getResult().getCarlist().get(position).getDealer();
        viewHolder.seriesname.setText(carlistBean.getSeriesname());
        viewHolder.specname.setText(carlistBean.getSpecname());
        viewHolder.dealerprice.setText(carlistBean.getDealerprice() + "万");
        viewHolder.fctprice.setText(carlistBean.getFctprice() + "万");
        viewHolder.orderranger.setText(carlistBean.getOrderrange());

        NetTool netTool = new NetTool();
        netTool.getImage(morkDownBean.getResult().getCarlist().get(position).getSpecpic(), viewHolder.specpic);

        viewHolder.city.setText(dealerBean.getCity());
        viewHolder.shortname.setText(dealerBean.getShortname());
        viewHolder.distance.setText(dealerBean.getDistance());

        return convertView;
    }

    public class ViewHolder{
        ImageView specpic ;
        TextView seriesname;
        TextView specname;
        TextView dealerprice;
        TextView fctprice;
        TextView city;
        TextView shortname;
        TextView distance;
        TextView orderranger;
        public ViewHolder(View convertView){
            specpic = (ImageView) convertView.findViewById(R.id.specpic_findcar_morkdown_item);
            seriesname = (TextView) convertView.findViewById(R.id.seriesname_findcar_morkdown_item);
            specname = (TextView) convertView.findViewById(R.id.specname_findcar_morkdown_item);
            dealerprice = (TextView) convertView.findViewById(R.id.dealerprice_findcar_morkdown_item);
            fctprice = (TextView) convertView.findViewById(R.id.fctprice_findcar_morkdown_item);
            city = (TextView) convertView.findViewById(R.id.city_findcar_morkdown_item);
            shortname = (TextView) convertView.findViewById(R.id.shortname_findcar_morkdown_item);
            distance = (TextView) convertView.findViewById(R.id.distance_findcar_morkdown_item);
            orderranger = (TextView) convertView.findViewById(R.id.orderrange_findcar_morkdown_item);
        }
    }

}
